package com.houtai.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.houtai.commonutils.Result;
import com.houtai.eduservice.client.VodClient;
import com.houtai.eduservice.config.OperationLogDesc;
import com.houtai.eduservice.entity.EduChapter;
import com.houtai.eduservice.entity.EduVideo;
import com.houtai.eduservice.service.EduVideoService;
import com.houtai.servicebase.exceptionhandler.DException;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-11-20
 */
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    @ApiOperation(value = "添加小节")
    @OperationLogDesc(module = "课程模块",description = "添加小节")
    public Result addVideo(@RequestBody @Valid EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return Result.ok();
    }
    //删除小节及其对于视频
    @DeleteMapping("{id}")
    @ApiOperation(value = "删除小节")
    @OperationLogDesc(module = "课程模块",description = "删除小节")
    public Result addVideo(@PathVariable Integer id){
        //根据小节ID得到视频ID
        EduVideo eduVideo= eduVideoService.getById(id);
        String videoSourceId=eduVideo.getVideoSourceId();
        //判断
        if(!StringUtils.isEmpty(videoSourceId)){
            Result result= vodClient.removeVideo(videoSourceId);
            if(result.getCode()==20001){
                throw new DException(20001,"删除失败，服务器挂掉啦！");
            }
        }
        eduVideoService.removeById(id);
        return Result.ok();
    }
    //修改小节
    //根据id查询
    @GetMapping("getVideoInfo/{videoId}")
    @ApiOperation(value = "获取小节信息")
    @OperationLogDesc(module = "课程模块",description = "获取小节信息")
    public Result getVideoInfo(@PathVariable Integer videoId){
        EduVideo eduVideo=eduVideoService.getById(videoId);
        return Result.ok().data("video",eduVideo);
    }
    //修改小节
    @PostMapping("updateVideo")
    @ApiOperation(value = "修改小节")
    @OperationLogDesc(module = "课程模块",description = "修改小节")
    public Result updateVideo(@RequestBody @Valid EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return Result.ok();
    }

}

